package com.bootdo.phry.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.common.domain.FileDO;
import com.bootdo.common.enums.UserType;
import com.bootdo.common.service.FileService;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.phry.dto.UserInfoDTO;
import com.bootdo.common.enums.Status;
import com.bootdo.system.domain.DeptDO;
import com.bootdo.system.service.DeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.phry.service.UserInfoService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.phry.dto.LeaveDTO;
import com.bootdo.phry.service.LeaveService;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-05-22 10:43:57
 */
 
@Controller
@RequestMapping("/phry/user")
public class UserInfoController {
	@Autowired
	private UserInfoService userService;
	@Autowired
	private DeptService deptService;
	@Autowired
	private FileService sysFileService;

	@GetMapping()
	@RequiresPermissions("phry:user:user")
	String User(){
	    return "phry/userinfo/user";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("phry:user:user")
	public PageUtils list(@RequestParam Map<String, Object> params){

        Query query = new Query(params);
		List<UserInfoDTO> userList = userService.list(query);
		userList.forEach(e->{
			e.setDeptDO(deptService.get(e.getDeptId()));
		});
		int total = userService.count(query);
		PageUtils pageUtils = new PageUtils(userList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("phry:user:add")
	String add(){
	    return "phry/userinfo/add";
	}

	@GetMapping("/doUserMain/{userId}")
	String doUserMain(Model model,@PathVariable("userId") Long userId){
		model.addAttribute("userId",userId);
		return "phry/userinfo/userinfo/doUserMain";
	}

	@GetMapping("/showUserInfo/{userId}")
	String showUserInfo(Model model,@PathVariable("userId") Long userId){
		model.addAttribute("userInfoDTO",userService.get(userId));
		return "phry/userinfo/userinfo/showUserInfo";
	}

	@GetMapping("/edit/{userId}")
	@RequiresPermissions("phry:user:edit")
	String edit(@PathVariable("userId") Long userId,Model model){
		UserInfoDTO user = userService.get(userId);
		DeptDO deptDO = deptService.get(user.getDeptId());
		if(deptDO!=null){
			user.setDeptName(deptDO.getName());
		}
		model.addAttribute("user", user);
		FileDO fileDO = sysFileService.get(user.getPicId());
		if(fileDO==null){
			fileDO = new FileDO();
			fileDO.setUrl("/img/backg01.jpg");
		}
		model.addAttribute("fileDO",fileDO);

		FileDO fileDO1 = sysFileService.get(user.getFileId());
		if(fileDO1==null){
			fileDO1 = new FileDO();
			fileDO1.setId(0l);
		}
		model.addAttribute("fileDO1",fileDO1);
	    return "phry/userinfo/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("phry:user:add")
	public R save( UserInfoDTO user) throws ParseException {

		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date=new Date();
		java.util.Date mydate= myFormatter.parse(myFormatter.format(user.getBirth()));
		long day=(date.getTime()-mydate.getTime())/(24*60*60*1000) + 1;
		int year= (int) Math.floor(day/365);
		user.setAge(year);
		user.setGmtCreate(new Date());
		user.setGmtModified(new Date());
		user.setStatus(Status.正常.getCode());
		R r = userService.save(user);
		return r;
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("phry:user:edit")
	public R update( UserInfoDTO user) throws ParseException{
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date=new Date();
		java.util.Date mydate= myFormatter.parse(myFormatter.format(user.getBirth()));
		long day=(date.getTime()-mydate.getTime())/(24*60*60*1000) + 1;
		int year= (int) Math.floor(day/365);
		user.setAge(year);

		user.setGmtModified(new Date());
		return userService.update(user);
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("phry:user:remove")
	public R remove( Long userId){
		UserInfoDTO user = new UserInfoDTO();
		user.setUserId(userId);
		user.setStatus(Status.禁用.getCode());
		return userService.update(user);
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("phry:user:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] userIds){
		userService.batchRemove(userIds);
		for (Long userId:userIds) {
			UserInfoDTO user = new UserInfoDTO();
			user.setUserId(userId);
			user.setStatus(Status.禁用.getCode());
			userService.update(user);
		}
		return R.ok();
	}
	//性别分析饼状图
	@GetMapping("/sexreport")
	@RequiresPermissions("phry:user:sexreport")
	String sexreport(){
	    return "phry/userinfo/sexreport";
	}

	@ResponseBody
	@PostMapping(value="/sexreportList")
	@RequiresPermissions("phry:user:sexreport")
	public R sexreportList(){
		//查询列表数据
	
		int man = userService.mancount();
		int woman=userService.womancount();
		
		return R.ok().put("man", man).put("woman", woman);
	}

	@GetMapping("/showFile/{fileId}")
	String showFile(@PathVariable("fileId") Long fileId,Model model){

		FileDO fileDO = sysFileService.get(fileId);
		model.addAttribute("fileDO",fileDO);
		return "phry/userinfo/userinfo/showFile";
	}
	
	
}
